import { observable, autorun, makeObservable, computed, action, flow, makeAutoObservable } from 'mobx';

class Num {
  value
  PI = 3.14
  constructor(value) {
    // makeObservable(this, {
    //   value: observable,
    //   double: computed,
    //   // action 1、内部使用事务的机制，内部改变不可观察，只有这个动作执行完了才观察到 2、只在action中修改状态，使状态的修改可追踪，好排查bug
    //   add: action,
    //   fetch: flow.bound,
    // })
    makeAutoObservable(this, { PI: false }, { autoBind: true });
    this.value = value;
  }
  get double() {
    return this.value * 2;
  }
  add() {
    this.value++;
  }
  *fetch() {
    let amount = yield new Promise((resolve) => setTimeout(() => resolve(5), 1000));
    this.value += amount;
  }
}

let num = new Num(1);

autorun(() => {
  console.log(num.value);
})
const fetch = num.fetch;
fetch();